from sqlalchemy import or_, exc

from app.config.db import DBUtils
from app.model.SysReportModel import SysReportModel


class SysReportService:

    def __init__(self):
        self.db_utils = DBUtils()

    # 根据uuid获取文件名称
    def select_file_by_uuid(self, session, uuid):
        try:
            filename = session.query(SysReportModel.FILE_NAME).filter(SysReportModel.FILE_UUID == uuid).one()[0]
            return filename
        finally:
            self.db_utils.remove_session()

    # 新增报告信息
    def insert_data(self, session, sysReportModel):
        try:
            session.add(sysReportModel)
            session.commit()
            return 1
        except exc.SQLAlchemyError as e:
            try:
                self.db_utils.rollback()
            except Exception as rollback_error:
                print(f"Failed to rollback transaction: {rollback_error}")
            return 0
        finally:
            try:
                self.db_utils.remove_session()
            except Exception as remove_error:
                print(f"Failed to rollback transaction: {remove_error}")
